package com.gree.common.utils;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;;

/**
 * 包路径: com.gree.util
 * 项目名: scm-api
 * 创建时间: 2021-06-18 8:47
 * @author fyb
 * 描述: TODO  List 转String And 拼接 sql in ('1','2')
 **/
public class StringHelper {
    /**
     * List<String>转换为String
     * @param list
     * @return TODO 1,2,3.....
     */
    public static String listToString(List<String> list) {
        if (list.isEmpty()) {
            return null;
        }
        String ss = "";
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            String error = (String) iterator.next();

            ss = error + "," + ss;
        }
        ss = ss.substring(0, ss.length() - 1);
        return ss;
    }
    /**
     * List<String>转换为String
     * 使用 StringJoiner java8 拼接
     * @param list
     * @return TODO 1,2,3.....
     */
    public static String listToStringJoiner(List<String> list) {
        return  String.join(",", list);
    }

    /**
     * List<String>转换为String
     * @param list
     * @return TODO '1','2','3'
     */
    public static String listToStringNew(List<String> list) {
        if (list.isEmpty()) {
            return null;
        }
        String ss = "";
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            String error = (String) iterator.next();

            ss = "'" + error + "'," + ss;
        }
        ss = ss.substring(0, ss.length() - 1);
        return ss;
    }

    /**
     * List<String>转换为String
     * 使用 StringJoiner java8 拼接
     * @param list
     * @return TODO '1','2','3'
     */
    public static String listToStringNewJoiner(List<String> list) {

        return "'"+String.join("','",list)+ "'";
    }

    /**
     * List<String>转换为String
     * 使用 StringJoiner java8 拼接
     * @param id  sql中的id
     * @param list
     * @return TODO id in('0','1','2','3','4','5')
     */
    public static String listToStringIdJoiner(String id , List<String> list) {

        return StringHelper.join("','",id+" in('","')",list);
    }

    /**
     * 重写join方法 ，携带两个参数
     * @param delimiter
     * @param prefix
     * @param suffix
     * @param elements
     * @return
     */
    public static String join(CharSequence delimiter,CharSequence prefix,CharSequence suffix,
                              Iterable<? extends CharSequence> elements) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        StringJoiner joiner = new StringJoiner(delimiter,prefix,suffix);
        for (CharSequence cs: elements) {
            joiner.add(cs);
        }
        return joiner.toString();
    }
//        #endregion

    /**
     * IN 查询时出现ORA-01795:列表中的最大表达式数为1000解决方法
     * @param sqlAndId select * from table where id
     * @param list
     * @return TODO select * from table where id in ('1','2',.....)
     */
    public static String getStringIdList(String sqlAndId, List<String> list) {
        StringBuilder sb = new StringBuilder();
        String returnString = "";
        if (list.size() == 0 || null == list) {
            returnString = sb.append(sqlAndId).append("=''").toString();
        }
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                sb.append(sqlAndId);
                sb.append(" in (");
            }
            sb.append("'");
            sb.append(list.get(i));
            sb.append("'");
            if (i >= 900 && i < list.size() - 1) {
                if (i % 900 == 0) {
                    sb.append(") \nunion all\n ");
                    sb.append(sqlAndId);
                    sb.append(" in (");
                } else {
                    sb.append(",");
                }
            } else {
                if (i < list.size() - 1) {
                    sb.append(",");
                }
            }
            if (i == list.size() - 1) {
                sb.append(")");
            }
        }
        returnString = sb.toString();
        return returnString;
    }
	 /**
     * TODO in中的参数以 900，(中间使用 union all)，进行在一次sql(参数 以 1000开始)的拼接
     * @param sql select * from table where
     * @param ids  id ->(orno)
     *              TODO 在循环开始时进行拼接 in(
     *              TODO 在中间拼接 'C01287922','A00262273'等等.....
     *              TODO 在循环结尾时进行拼接 )
     * @param sql2   group by *.....
     * @param list   list<?> 集合都可以传 records
     * @param methodName 方法名称 TODO 方法名称没有参数的不用传递 args
     * @param args  方法参数
     *  TODO 例子：-> String str = StringHelper.getStringListObj(sql,"t1.djid","ORDER BY t1.edat DESC",records,"getString","djid");
     * @return TODO select * from table where t1.djid in ('1','2','3') ORDER BY t1.edat DESC....
      *         union all
     * @return TODO select * from table where t1.djid in ('901','902','903') ORDER BY t1.edat DESC....
     */
    public static String getStringListObj(String sql ,String ids,String sql2,List<?> list,String methodName,Object... args) {
        StringBuilder sb = new StringBuilder();
        String returnString = "";
        if (list.size() == 0 || null == list) {
            sb.append(sql);
            returnString = sb.append(ids).append("=('')").toString();
        }
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                sb.append(sql);
                sb.append(ids);
                sb.append(" in (");
            }
            sb.append("'");
            sb.append(invoke(list.get(i),methodName,false,args));
            sb.append("'");
            if (i >= 900 && i < list.size() - 1) {
                if (i % 900 == 0) {
                    sb.append(")").append(sql2);
                    sb.append(" \n union all \n ");
                    sb.append(sql);
                    sb.append(ids);
                    sb.append(" in (");
                } else {
                    sb.append(",");
                }
            } else {
                if (i < list.size() - 1) {
                    sb.append(",");
                }
            }
            if (i == list.size() - 1) {
                sb.append(")");
            }
        }
        sb.append(sql2);
        returnString = sb.toString();
        return returnString;
    }
    /**
     *
     * @param object 对象
     * @param methodName 方法名称
     * @param isDeclared 是否是 私有方法 true 私有 ,false 公共
     * @param args 方法参数
     * @return
     * @throws Exception
     */
    public static Object invoke(Object object, String methodName, boolean isDeclared, Object... args) {
        try {
            Class[] parameterTypes = null;
            if (args!=null){
                // 获取要执行方法的参数类型
                parameterTypes = new Class[args.length];
                for (int i = 0; i < args.length; i++) {
                    parameterTypes[i] = args[i].getClass();
                }
            }
            if (!isDeclared){
                // 获取方法(包括父类的，但不能获取私有方法)
                Method method = null;
                method = object.getClass().getMethod(methodName, parameterTypes);
                // 执行方法
                return method.invoke(object, args);
            }else{
                // 获取方法(包括父类的，获取私有名称)
                Method method = object.getClass().getDeclaredMethod(methodName, parameterTypes);
                method.setAccessible(true);
                return method.invoke(object,args);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
}
